<?php
namespace app\web\controller;


use think\Db;
use app\web\constant\CommonConstant;

class WorkerType extends Auth
{

    /**
     * 获取工种列表
     */
    public function lists () {

        if ($this->request->isPost()) {

            $post = $this->request->post();
            if (empty($post['limit'])) {
                $this->result('参数缺失', '', 204);
            }
            $data = Db::name('qt_work_type')
                ->where(['store_id'=>$this->userInfo['store_id']])
                ->order('id desc')
                ->paginate($post['limit']);

            $data = $this->treeNode($data);

            $list = [];
            $num = 0;
            foreach ($data as $k => $v) {
                $list[$num]['id'] = $v['id'];
                $list[$num]['title'] = $v['title'];
                $list[$num]['sort'] = $v['sort'];
                $list[$num]['pid'] = "顶级工种";
                if (!empty($v['childer'])) {
                    foreach ($v['childer'] as $c_k => $c_v) {
                        $num ++;
                        $list[$num]['id'] = $c_v['id'];
                        $list[$num]['title'] = $c_v['title'];
                        $list[$num]['sort'] = $c_v['sort'];
                        $list[$num]['pid'] = $v['title'];
                    }
                }
                $num ++;
            }
            $this->result('查询成功', $list, 200);
        }
        $this->result('无效的请求', '', 204);

    }

    /**
     * 整理排序所有分类
     * @param  array   $data       从数据库获取的分类
     * @param  integer $parentId 父id,默认一级分类
     * @return array
     */
    private function treeNode($data,$parentId = 0)
    {
        // 用于保存整理好的分类节点
        $node = [];
        // 循环所有分类
        foreach ($data as $key => $value) {
            // 如果当前分类的父id等于要寻找的父id则写入$node数组,并寻找当前分类id下的所有子分类
            if($parentId == $value ['pid']) {
                $node [$key] = $value;
                $node [$key] ['childer'] = $this->treeNode($data,$value ['id']);
            }
        }
        return $node;
    }

    /**
     * 添加工种
     */
    public function addType()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $insertData['store_id'] = $this->userInfo['store_id'];
            $insertData['title'] = $getData['title'];
            $insertData['pid'] = $getData['pid'];
            $insertData['sort'] = $getData['sort'];
            $insertData['created_time'] = time();

            $is_exits = Db::name('qt_work_type')
                ->field('id,title,pid')
                ->where([
                    'title'=>$getData['title'],
                    'store_id'=>$this->userInfo['store_id']
                ])
                ->find();
            if (!empty($is_exits)) {
                $this->result('不允许添加同名工种', '', 204);
            }

            $insert = Db::name('qt_work_type')->insert($insertData);
            if ($insert) {
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);

        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 获取工种类型
     */
    public function getType()
    {
        if ($this->request->isPost()) {
            $data = Db::name('qt_work_type')
                ->field('id,title,pid')
                ->where([
                    'pid' => CommonConstant::WORKER_TYPE_LEVEL_ZERO,
                    'store_id'=>$this->userInfo['store_id']
                ])
                ->select();
            if ($data) {
                array_unshift($data, ['id' => 0, 'title' => '顶级分类', 'pid' => 0]);
                $this->result('查询成功', $data, 200);
            }
        }
        $this->result('查询失败', '', 204);
    }


    /**
     * 修改工种分类信息
     */
    public function editWorkerType(){

        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $insertData['id'] = $getData['id'];
            $insertData['title'] = $getData['title'];
            $insertData['pid'] = $getData['pid'];
            $insertData['sort'] = $getData['sort'];
            $insertData['updated_time'] = time();
            $is_exits = Db::name('qt_work_type')
                ->field('id,title,pid')
                ->where([
                    'title'=>$getData['title'],
                    'store_id'=>$this->userInfo['store_id']
                ])
                ->where('id','<>',$getData['id'])
                ->find();
            if (!empty($is_exits)) {
                $this->result('存在同名工种', '', 204);
            }

            $insert = Db::name('qt_work_type')->update($insertData);
            if ($insert) {
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败', '', 204);

        }
        $this->result('无效的请求', '', 204);

    }


    /**
     * 获取修改工种分类详情
     */
    public function getWorkerTypeInfo(){
        if ($this->request->isPost()) {
            $post = $this->request->post();
            if (!empty($post['id'])) {
                $data = Db::name('qt_work_type')
                    ->where(['id'=>$post['id']])
                    ->find();
                if ($data) {
                    $this->result('查询成功', $data, 200);
                }else{
                    $this->result('没有查询到对应ID的数据', '', 204);
                }
            }
        }

        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除分类接口
     */
    public function remove () {

        if ($this->request->isPost()) {
            $post = $this->request->post();
            if (!empty($post['id'])) {
                $data = Db::name('qt_work_type')
                    ->where(['id'=>$post['id']])
                    ->find();
                if ($data) {
                    $p_data = Db::name('qt_work_type')
                        ->where(['pid'=>$data['id']])
                        ->find();
                    if (!empty($p_data)) {
                        $this->result('该分类下有未删除的工种分类不能删除', '', 204);
                    }
                    Db::name('qt_work_type')->where(['id'=>$post['id']])->delete();
                    $this->result('删除成功', '', 200);
                }else{
                    $this->result('没有查询到对应ID的数据', '', 204);
                }
            }
        }
        $this->result('无效的请求', '', 204);

    }

}